package com.itheima.demo;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Demo01 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s = "诸葛亮";

        //默认使用UTF-8
        byte[] bytes1 = s.getBytes();
        //指定GBK码表编码格式
        byte[] bytes2 = s.getBytes("GBK");

        System.out.println(Arrays.toString(bytes1));
        System.out.println(Arrays.toString(bytes2));

        //UTF-8 一个中文三个字节，所以数组长度为9
        byte[] bytes3 = {-24, -81, -72, -24, -111, -101, -28, -70, -82};
        //GBK 一个中文两个字节，所以数组长度为6
        byte[] bytes4 = {-42, -18, -72, -16, -63, -63};

        //默认使用UTF-8
        String s1 = new String(bytes3);
        //指定GBK码表编码格式
        String s2 = new String(bytes4,"GBK");
        System.out.println(s1);
        System.out.println(s2);
    }
}
